﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.Sql;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Web.SessionState;

/// <summary>
/// Summary description for productBv_PV
/// </summary>
namespace Bestlife.ERP.DataAccess
{
    public class product
    {
        public static Boolean update(Bestlife.ERP.Entities.product product)//insert 
        {
            Boolean status = false;
            DateTime date = System.DateTime.Now;

            String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
            Bestlife.ERP.DataAccess.clsCon.connect(connStr);

            decimal bv = Bestlife.ERP.DataAccess.product.setpvbv(product.Point_Value, product.Precentage);
            string sql = "INSERT INTO [BestlifeDB].[dbo].[Tbl_Product]([Pro_ID],[Code],[Description],[Pro_Name],[Pro_Price],[Ret_Price],[Pur_Price],[Point_Value],[Pro_Type],[Cat_ID],[Sub_Cat],[Percentage],[Business_Value],[Date_Create],[Last_Modify])VALUES ('"+product.Pro_ID+"','"+product.Pro_Code+"','"+product.Pro_Des+"','"+product.Pro_Name+"','"+product.Pro_Price+"','"+product.Ret_Price+"','"+product.Pur_Price+"','"+product.Point_Value+"','"+product.Pro_Type+"','"+product.Cat_ID+"','"+product.Sub_ID+"','"+product.Precentage+"','"+bv+"','"+date+"','"+date+"')";
         
            Boolean add=Bestlife.ERP.DataAccess.clsCon.AddEditDel(sql);
             if (add)
             {
                 status = true;
                 return status;
             }
             return status;

             }

        public static Boolean insertproduct(Bestlife.ERP.Entities.product insert)//update
        {
            Boolean status = false;
            DateTime date = System.DateTime.Now;


            String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
            Bestlife.ERP.DataAccess.clsCon.connect(connStr);
            
            string sql = "UPDATE Tbl_Product SET Code ='" + insert.Pro_Code + "',Description ='" + insert.Pro_Des + "',Pro_Name ='" + insert.Pro_Name + "',Pro_Price ='" + insert.Pro_Price + "',Ret_Price ='" + insert.Ret_Price + "',Pur_Price='" + insert.Pur_Price + "',Point_Value ='" + insert.Point_Value + "',Pro_Type ='" + insert.Pro_Type + "',Cat_ID ='" + insert.Cat_ID + "',Sub_Cat ='" + insert.Sub_ID + "',Last_Modify ='" + date + "' WHERE Pro_ID ='" + insert.Pro_ID + "' ";
            Boolean edit=Bestlife.ERP.DataAccess.clsCon.AddEditDel(sql);
            if (edit)
             {
                 status = true;
                 return status;
             }
             return status;

             }
                 
        public static Boolean image(string[] imgList,string pid)//insert image
        {
            Boolean status = false;
            DateTime date = System.DateTime.Now;


            String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
            Bestlife.ERP.DataAccess.clsCon.connect(connStr);

            string sql="UPDATE [BestlifeDB].[dbo].[Tbl_Product] SET [Pro_Image] ='"+imgList[0]+"',[Pro_Image2] ='"+imgList[1]+"',[Pro_Image3] = '"+imgList[2]+"',[Pro_Image4] ='"+imgList[3]+"',[Last_Modify] ='"+date+"' WHERE Pro_ID ='" +pid + "' ";
            Boolean edit=Bestlife.ERP.DataAccess.clsCon.AddEditDel(sql);
                      
            if (edit)
             {
                 status = true;
                 return status;
             }
             return status;

             }
      
        public static decimal setpvbv(decimal pvvalue, int precent)//calculate bussiness value
        {
            
            decimal bv = (pvvalue * precent)/100;
            return bv;

        }

        public static Boolean status()
        {
            SqlDataReader rdR;
            try
            {
                DateTime date = System.DateTime.Now;

                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                string Show = "Show";
                String Ssql = "SELECT [Status] FROM [BestlifeDB].[dbo].[Tbl_Category] where Status='" + Show + "'";

                rdR = clsCon.ReadTable(Ssql);

                if (rdR.Read())
                {
                    return true;

                }
                rdR.Close();
            }
            catch(Exception)
            {
            
            }
            return false;
        }

        public static string contctperson(string supid)
        {
            SqlDataReader rdR;
            try
            {
                DateTime date = System.DateTime.Now;

                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);


                String Ssql = "SELECT [Contact] FROM [Tbl_Supplier] where [Sup_ID]='" + supid + "'";

                rdR = clsCon.ReadTable(Ssql);

                if (rdR.Read())
                {
                    string contact = rdR["contact"].ToString();
                    return contact;
                }
                rdR.Close();
            }
            catch(Exception)
            {
            }
            string error = "";
            return error;
        }

        public static decimal pro_price(string name)
        {
            SqlDataReader rdR;
            try{
            DateTime date = System.DateTime.Now;

            String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
            Bestlife.ERP.DataAccess.clsCon.connect(connStr);


            String Ssql = "SELECT [Pur_Price] FROM [BestlifeDB].[dbo].[Tbl_Product] where Pro_Name='" + name + "'";
           
            rdR = clsCon.ReadTable(Ssql);

            if (rdR.Read())
            {
                decimal price =decimal.Parse(rdR["Pur_Price"].ToString());
                return price;
            }
            rdR.Close();
            }
            catch(Exception)

            {
            }
           
                decimal error = 0;
                return error;
            
        }

        public static string catsub(string cat)
        {
            SqlDataReader rdR;
            try
            {
                DateTime date = System.DateTime.Now;

                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);


                String Ssql = "SELECT [Sub_Cat] FROM [BestlifeDB].[dbo].[Tbl_Sub_Category] where [Cat_ID]='" + cat + "'";

                rdR = clsCon.ReadTable(Ssql);

                if (rdR.Read())
                {
                    string sub = (rdR["Sub_Cat"].ToString());
                    return sub;
                }
                rdR.Close();
            }
            catch(Exception)
            {
                
            }
            string error = "error";
            return error; 
        }
    }
}
